const initialState = {
  penddingList:[],
  finishedList:[]
}

export function todoReducer(state=initialState,action){
  console.log(action)
  switch(action.type){
    case "addTodo":
      var flag = false;
      state.penddingList.forEach((ele)=>{
        if(ele.text === action.text){
          window.alert("不能添加相同todo")
          flag=true
        }
      })
      if(!flag){
        var todo = {
          text:action.text,
          status:"PENDDING"
        }
        state.penddingList.push(todo)
      }
      return {
        penddingList:[...state.penddingList],
        finishedList:state.finishedList
      };
    case "penddingToFinished":
      state.penddingList.forEach((ele,index)=>{
        if(ele.text===action.todo.text){
          state.penddingList.splice(index,1);
        }
      })

      action.todo.status = "FINISHED"
      state.finishedList.push(action.todo)
      return {
        penddingList:[...state.penddingList],
        finishedList:[...state.finishedList]
      };
    default:
      return state;
  }
}